//
//  ZXLRadioPlayController.m
//  EPA
//
//  Created by 朱兴隆 on 16/4/21.
//  Copyright © 2016年 朱兴隆. All rights reserved.
//

#import "ZXLRadioPlayController.h"
#import "ZXLRadioListModel.h"
#import "ZXLAVManager.h"

#import "LORefresh.h"
@interface ZXLRadioPlayController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
// 当前时间
@property (weak, nonatomic) IBOutlet UILabel *currentLabel;
// 播放进度
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
// 总时长
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;
// 计时器
@property (nonatomic, strong) NSTimer *playTimer;

@property (weak, nonatomic) IBOutlet UIButton *playButton;




@end

@implementation ZXLRadioPlayController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rateSlider.value = 0;
    
    ZXLRadioListModel *model = self.radioArray[self.currentIndex];
    
    self.titleLabel.text = model.title;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    
    [ZXLAVManager shareManager].currentIndex = self.currentIndex;
    
    NSMutableArray *itemArray = [NSMutableArray new];
    for (ZXLRadioListModel *model in self.radioArray) {
        NSURL *musicUrl = [NSURL URLWithString:model.playInfoModel.musicUrl];
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:musicUrl];
        [itemArray addObject:item];
    }
    
    // AVPlayerItem数组
    [ZXLAVManager shareManager].itemArray = itemArray;
    
    [[ZXLAVManager shareManager] play];
    
    // 创建timer
    self.playTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    self.navigationController.navigationBar.translucent = NO;
    
    
    // Do any additional setup after loading the view from its nib.
}
// currentLabel和totalLabel
- (void)timerAction {
    float current = [ZXLAVManager shareManager].player.currentTime.value / [ZXLAVManager shareManager].player.currentTime.timescale;
    
    NSString *currentStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)current/60,(NSInteger)current%60];
    
    self.currentLabel.text = currentStr;
    
    float total = 0;
    if ([ZXLAVManager shareManager].player.currentItem.duration.timescale > 0) {
        total = [ZXLAVManager shareManager].player.currentItem.duration.value / [ZXLAVManager shareManager].player.currentItem.duration.timescale;
    }
    
    
    NSString *totalStr = [NSString stringWithFormat:@"%02ld,%02ld",(NSInteger)total/60,(NSInteger)total%60];
    
    self.totalLabel.text = totalStr;
    
    if (total > 0) {
        float rate = current /total;
        self.rateSlider.value = rate;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 上一曲
- (IBAction)didClickPreviousButton:(UIButton *)sender {
    [[ZXLAVManager shareManager] previous];
    // 图片和标题
    self.currentIndex --;
    if (self.currentIndex < 0) {
        self.currentIndex = self.radioArray.count - 1;
    }
    ZXLRadioListModel *model = self.radioArray[self.currentIndex];
    
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
    
    [self.playButton setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
}
//  播放
- (IBAction)didClickPlayButton:(UIButton *)sender {
    if ([ZXLAVManager shareManager].player.rate == 0) {
        [[ZXLAVManager shareManager] play];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
    }else {
        [[ZXLAVManager shareManager] pause];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
    }
}
// 下一曲
- (IBAction)didClickNextButton:(UIButton *)sender {
    [[ZXLAVManager shareManager] next];
    // 图片标题
    self.currentIndex ++;
    if (self.currentIndex >= self.radioArray.count) {
        self.currentIndex = 0;
    }
    ZXLRadioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
    
    [self.playButton setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
}
- (IBAction)didChangeRateAction:(UISlider *)sender {
    CMTime changeTime = CMTimeMake(sender.value * [ZXLAVManager shareManager].player.currentItem.duration.value, [ZXLAVManager shareManager].player.currentItem.duration.timescale);
    
    // seekToTime
    [[ZXLAVManager shareManager] seekToTime:changeTime];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
